using Admin.Application.Dto.User;
using Admin.Application.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Admin.Application.Dto;

namespace Admin.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class UsersController(IUserService userService) : ControllerBase
{
    private readonly IUserService _userService = userService;
    [HttpGet("list")]
    public async Task<IActionResult> GetAll(int pageIndex = 0, int pageSize = 10)
    {
        var departments = await _userService.GetPagedUserAsync(pageIndex, pageSize);
        return Ok(departments);
    }

    [HttpGet("get/{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var department = await _userService.GetUserByIdAsync(id);
        return Ok(department);
    }

    [HttpDelete("delete/{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var result = await _userService.DeleteUserAsync(id);
        return Ok(result);
    }

    [HttpPut("update/info/{id}")]
    public async Task<IActionResult> Update(Guid id, UserInfoDto userDto)
    {
        var result = await _userService.UpdateUserInfoAsync(id, userDto);
        return Ok(result);
    }

    [HttpPut("update/password/{id}")]
    public async Task<IActionResult> UpdatePassword(Guid id, UserPwdDto password)
    {
        var result = await _userService.UpdateUserPwdAsync(id, password);
        return Ok(result);
    }

    [HttpPut("update/assign/{userId}/{roleId}")]
    public async Task<IActionResult> AssignRolesToUser(Guid userId, Guid roleId)
    {
        var result = await _userService.AssignRolesToUserAsync(userId, roleId);
        return Ok(result);
    }

    [HttpPut("update/remove/{userId}/{roleId}")]
    public async Task<IActionResult> RemoveRolesToUser(Guid userId, Guid roleId)
    {
        var result = await _userService.RemoveRolesFromUserAsync(userId, roleId);
        return Ok(result);
    }

    [HttpPut("update/isactive/{id}")]
    public async Task<IActionResult> UpdateIsActive(Guid id, ActiveDto activeDto)
    {
        var result = await _userService.UpdateIsActive(id, activeDto);
        return Ok(result);
    }

    [HttpPut("update/Department/{userId}/{departmentId?}")]
    public async Task<IActionResult> UpdateDepartment(Guid userId, Guid? departmentId)
    {
        var result = await _userService.UpdateUserDepartmentIdAsync(userId, departmentId);
        return Ok(result);
    }
}